/**
 * Trigger the "event" for recognizing scrolling of the FB canvas.
 */
var lastScrollTop;
setInterval(function(){
		if(window.FB){
			FB.Canvas.getPageInfo(function(info){
					if(lastScrollTop != info.scrollTop){
						showImagesThatAreNowInView();
					}
					lastScrollTop=info.scrollTop;
			});
		}
},100);

/**
 * Check for unloaded images that entered the view due to scrolling.
 */
function showImagesThatAreNowInView(){
	if(window.FB){
		FB.Canvas.getPageInfo(function(info){
				//Unloaded `img` tags has their image URL in `unloaded_src`
				//instead of `src`.
				var imagesNotYetLoaded=$('img[unloaded_src]');
				var imagesNotYetLoadedInViewport=imagesNotYetLoaded.filter(function(){
						return $(this).position().top<=info.scrollTop+info.clientHeight &&
							   $(this).position().top+$(this).height()>=info.scrollTop;
				});
				imagesNotYetLoadedInViewport.each(function(){
						$(this).attr('src',$(this).attr('unloaded_src'));
						$(this).removeAttr('unloaded_src');
				});
		});
	}
}
